package com.freight.file.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.freight.core.common.exception.FreightException;
import com.freight.core.common.redis.RedisTemplateHelper;

import java.util.HashMap;

import static com.freight.file.util.BaiDuToken.getAuth;

/**
 * @author hytma
 */
public class IdCardOCRUtil {


    //百度地址
    public static String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";


    public static HashMap<String, Object> baiDuJustIdcard(String imgBase64, RedisTemplateHelper redisTemplate) {

        HashMap<String, Object> result = new HashMap<>();

        try {
            String param = "id_card_side=" + "front" + "&image=" + imgBase64;

            if(redisTemplate.get("baiduAccessToken")==null){
                getAuth(redisTemplate);
            }
            String accessToken = redisTemplate.get("baiduAccessToken");
            String res = HttpUtil.post(url, accessToken, param);
            System.out.println(res);

            JSONObject res_obj = JSON.parseObject(res);
            if(res_obj.containsKey("error_code")){
                throw new FreightException("图片不清楚，请重新上传");
            }
            JSONObject idcardjson = res_obj.getJSONObject("words_result");
            result.put("name", idcardjson.getJSONObject("姓名").getString("words"));
            result.put("nationality", idcardjson.getJSONObject("民族").getString("words"));
            result.put("address", idcardjson.getJSONObject("住址").getString("words"));
            result.put("sex", idcardjson.getJSONObject("性别").getString("words"));
            result.put("num", idcardjson.getJSONObject("公民身份号码").getString("words"));
            result.put("birth", idcardjson.getJSONObject("出生").getString("words"));
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public static HashMap<String, Object> baiDuBackIdcard(String imgBase64, RedisTemplateHelper redisTemplate) {
        // 请求url
        HashMap<String, Object> result = new HashMap<>();

        try {
            String param = "id_card_side=" + "back" + "&image=" + imgBase64;

            if(redisTemplate.get("baiduAccessToken")==null){
                getAuth(redisTemplate);
            }
            String accessToken = redisTemplate.get("baiduAccessToken");
            String res = HttpUtil.post(url, accessToken, param);
            System.out.println(res);

            JSONObject res_obj = JSON.parseObject(res);
            if(res_obj.containsKey("error_code")){
                throw new FreightException("图片不清楚，请重新上传");
            }
            JSONObject idcardjson = res_obj.getJSONObject("words_result");

            result.put("startDate", idcardjson.getJSONObject("签发日期").getString("words"));
            result.put("endDate", idcardjson.getJSONObject("失效日期").getString("words"));
            result.put("issue",idcardjson.getJSONObject("签发机关").getString("words"));
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
